package com.xcx.robot.mirai.factory.facade;

import cn.hutool.core.collection.CollectionUtil;
import com.xcx.robot.mirai.factory.BaseFacade;
import com.xcx.robot.mirai.key.KeyWord;
import com.xcx.robot.mirai.util.KeyUtils;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.message.MessageReceipt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 邢晨旭
 * @Date: 2021/5/26 16:18
 * @Description:
 */
@Service
@Slf4j
public class RecallFacade implements BaseFacade {
    public KeyWord keyWord = KeyWord.RECALL;

    public static Map<Long, List<MessageReceipt>> recall = new HashMap<>();

    @Override
    public void execute(Contact sender, Contact receiver, String content) {
        if (CollectionUtil.isEmpty(recall.get(receiver.getId()))) {
            return;
        }
        String keyByWord = KeyUtils.getKeyByWord(content, 2);
        log.info("keyByWord{}", keyByWord);
        if (StringUtils.isEmpty(keyByWord)) {
            keyByWord = "1";
        }
        if (StringUtils.isNumeric(keyByWord)) {
            try {
                List<MessageReceipt> receipts = recall.get(receiver.getId());
                log.info("撤回列表{}", receipts);
                MessageReceipt messageReceipt = receipts.get(receipts.size() - Integer.parseInt(keyByWord));
                messageReceipt.recall();
//                recall.get(receiver.getId()).remove(messageReceipt);
            } catch (Exception e) {
                log.error("撤回消息报错{}", e);
            }
        }
    }

    @Override
    public KeyWord keyWord() {
        return keyWord;
    }
}
